import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamDemo1 {
    public static void main(String[] args) {
        //1:avg
        int[] arr = {1,2,3,4,5,6};
        double asDouble = IntStream.of(arr).average().getAsDouble();
        System.out.println(asDouble);

        //2:sum
        int sum = IntStream.of(arr).sum();
        System.out.println(sum);

        //3:Stream 的创建
        List<String> list = new ArrayList<>();
        Stream<String> s1 = list.stream();
        Set<String> set = new HashSet<>();
        Stream<String> s2 = set.stream();

        //4
        String[] arr2 = {"1","2","3"};
        Stream<String> s4 = Arrays.stream(arr2);
        Stream<Integer> s5 = s4.map( i -> Integer.valueOf(i));
        System.out.println(s5.collect(Collectors.toList()));

        //5
        IntStream.of(1,2,3).map(i->2*i).forEach(System.out::println);

        //6
        String[] arr6 = {"1","2","3"};
        Stream<String> s6 = Arrays.stream(arr6);
        s6.mapToLong(i -> Long.parseLong(i)).forEach(System.out::println);

        //7
        Optional<Integer> optional = Stream.of(1,2,3,4).reduce((i,j) -> i+j);
        System.out.println("第7个函数计算结果："+optional.orElse(-1));

        //8
        Optional<String> optional2 = Stream.of("wwwjavaboyorg".split("")).reduce((i,j)-> i + "." + j);
        System.out.println(optional2.orElse(""));

        //9
        Stream<Integer> s9 = Stream.of(1,2,3,4);
        long count = s9.count();
        System.out.println(count);

        //10
        Stream<Integer> s10 = Stream.of(1,2,3,4);
        Optional<Integer> min = s10.min(Comparator.comparing(i->i));
        System.out.println("min.get = "+ min.get());


        for (int i=0;i<10;i++){
            Optional<Integer> first = Stream.of(1,2,3,4).parallel().findFirst();
            System.out.println("first.get() = "+first.get());
        }

        System.out.println("=================================================");

        for (int i = 0;i<10;i++){
            Optional<Integer> first1=Stream.of(1,2,3,4).parallel().findAny();
            System.out.println("first.get()="+first1.get());
        }

        //11
        boolean b =Stream.of(1,2,3,4).allMatch(i->i>5);
        System.out.println("b="+b);

        //12
        new Random().ints().limit(50).parallel().forEach(i->{
            System.out.println(Thread.currentThread().getName() + "---->" + i);
        });

        User user = new User();
        user.setUsername("zhang3");
        user.setAge(12);

        User user1 = new User();
        user1.setUsername("li4");
        user1.setAge(20);

        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        //13
        List<Integer> ages = users.stream().map(User::getAge).collect(Collectors.toList());
        System.out.println("ages = " + ages);

        Map<String,Object> map = users.stream().collect(Collectors.toMap(User::getUsername,User::getAge,(key1,key2) ->key2));
        System.out.println(map);

        Map<String,Object> maps = users.stream().collect(Collectors.toMap(User::getUsername, Function.identity(),(key1,key2)->key2));
        System.out.println(maps);
    }
}
